// 中间件middleware
import http from '../utils/httpclient'


export default function(middlewareAPI){
    return function(dispatch){
        return function(action){
            let {type,  url, data, method = 'get', name} = action;
            if(!url){
               return dispatch(action);
            }
            const [one_type,two_type, three_type] = action.type
            middlewareAPI.dispatch({ type: one_type });
            if(url){
                var Data= action.data ? action.data : {};
                return new Promise((resolve, reject) => {
                       http[method](url, Data).then(response => {
                           let _action = { type: two_type, result: response };
                           middlewareAPI.dispatch(_action);
                           resolve(response);//已成功的回调  resolved 参数将传递给对应的回调方法
                         }).catch(error => {
                           middlewareAPI.dispatch({
                             type: three_type
                           });
                           rejecte(error);
                           //已失败rejected的回调 参数将传递给对应的回调方法
                         });
                })

            }
        }
    }
}
